package com.unitechs.trap.excel.webapi.service.impl.entity;

import com.unitechs.trap.common.pojo.trap.entity.AllData;
import com.unitechs.trap.common.pojo.trap.entity.TrapVarBind;
import com.unitechs.trap.common.pojo.trap.entity.TrapVarName;
import com.unitechs.trap.common.pojo.trap.entity.VarData;
import com.unitechs.trap.excel.service.ITrapVarNameExcelService;
import com.unitechs.trap.excel.webapi.mapper.AllDataMapper;
import com.unitechs.trap.excel.webapi.mapper.ReadAlaOutMapper;
import com.unitechs.trap.excel.webapi.mapper.VarDataMapper;
import com.unitechs.trap.excel.webapi.utils.StreamUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@DubboService
@Slf4j
@Service
public class TrapVarNameExcelServiceImpl implements ITrapVarNameExcelService {
    @Autowired
    VarDataMapper varDataMapper;
    @Override
    public List<TrapVarName> listTrapVarName() {
        List<VarData> allData = varDataMapper.listVarData();
        allData=deduplicationWithJDBC(allData);
        List<TrapVarName> trapVarNames=new ArrayList<>();
        allData.forEach(data->{
            TrapVarName trapVarName=new TrapVarName();
            trapVarName.setVarName(data.getVarName());
            trapVarName.setVarOid(data.getVarOid());

            switch (data.getVarName().toLowerCase()){
                case "alarmcode":
                    trapVarName.setVarName("AlarmTypeID");
                    trapVarName.setIsAlarm("Y");
                    break;
                case "alarmneip":
                    trapVarName.setVarName("ResIP");
                    trapVarName.setIsAlarm("Y");
                    break;
                case "alarmstatus":
                case "ifadminstatus":
                    trapVarName.setVarName("AlarmStatus");
                    trapVarName.setIsAlarm("Y");
                    break;
                case "alarmintf":
                    trapVarName.setVarName("ResPara");
                    trapVarName.setIsAlarm("Y");
                    break;

                default:
                    trapVarName.setIsAlarm("N");

            }
            trapVarName.setIsIfindex("N");
            trapVarName.setTid(UUID.randomUUID().toString());
            LocalDateTime localDateTime=LocalDateTime.now();
            trapVarName.setCreateTime(localDateTime);
            trapVarName.setModifyTime(localDateTime);
            trapVarName.setAssignFunc("oidRawValue");
            trapVarName.setSpecialty("ALL");
            trapVarNames.add(trapVarName);
        });

        return trapVarNames;
    }

    private List<VarData> deduplication(List<VarData> varData){
        Set<String> uniqueOids = new HashSet<>();
        List<VarData> filteredList = new ArrayList<>();
        for (VarData s : varData) {
            String a=s.getVarOid()+s.getVarName();
            if (uniqueOids.add(a)) {
                filteredList.add(s);
            }

        }
        return filteredList;
    }

    @Autowired
    private ReadAlaOutMapper mapper;
    private List<VarData> deduplicationWithJDBC(List<VarData> varData){
        List<TrapVarName> trapVarNames = mapper.listTrapVarName();
        Set<String> uniqueOids = new HashSet<>();

        for (TrapVarName trapVarName:trapVarNames){
            uniqueOids.add(trapVarName.getVarOid()+trapVarName.getVarName());
        }

        List<VarData> filteredList = new ArrayList<>();
        for (VarData s : varData) {
            String a=s.getVarOid()+s.getVarName();
            if (uniqueOids.add(a)) {
                filteredList.add(s);
            }

        }
        return filteredList;

    }
}
